using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for Favourites.xaml
    /// </summary>

    public partial class Favourites : Canvas
    {
        ExportConfig exportWindow = null;

        public ExportConfig ExportWindow
        {
            get { return exportWindow; }
            set { exportWindow = value; }
        }
        public Favourites()
        {
            InitializeComponent();
            this.close.MouseLeftButtonUp += new MouseButtonEventHandler(close_MouseLeftButtonUp);
            this.detailSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(detailSlider_ValueChanged);
            
        }

        FanPanel fan;

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            fan = (FanPanel)sender;
            fan.Refresh += new RoutedEventHandler(fan_Refresh);
            UpdateCount();
        }

        void OnClick(object sender, MouseButtonEventArgs e)
        {
            if (!fan.IsWrapPanel)
            {
                this.grid.Width = 420;
                this.grid.Height = 260;
                this.text.Visibility = Visibility.Collapsed;
                fan.IsWrapPanel = true;
                this.BeginStoryboard((Storyboard)grid.FindResource("expandPanel"));
            }
        }

        void close_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Hide();
        }

        public void Hide()
        {
            fan.IsWrapPanel = false;
            detailSlider.Value = detailSlider.Maximum;
            this.BeginStoryboard((Storyboard)grid.FindResource("collapsePanel"));
        }

        void OnCompleted(object sender, RoutedEventArgs e)
        {
            if (!fan.IsWrapPanel && this.grid.Width != 230)
            {
                this.grid.Width = 230;
                this.grid.Height = 210;
                this.text.Visibility = Visibility.Visible;
                modal.Visibility = Visibility.Collapsed;
            }
        }

        void detailSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int n = (int)(detailSlider.Value * 100 / detailSlider.Maximum);
            textDetail.Text = String.Format("ZOOM: {0}%", n);
        }

        void fan_Refresh(object sender, RoutedEventArgs e)
        {
            UpdateCount();
        }

        void UpdateCount()
        {
            counter.Text = fan.Children.Count.ToString();
        }

        void OnItemClick(object sender, RoutedEventArgs e)
        {
            XmlDataProvider cvs = (XmlDataProvider)FindResource("Things");
            object o = cvs.Data;
            e.Handled = true;
        }

        System.Windows.Controls.Image currentImage = null;
        string currentPath = "";
        private void OnItemClick(object sender, MouseButtonEventArgs e)
        {
            
            System.Windows.Controls.Image image = sender as System.Windows.Controls.Image;

            string path =image.Source.ToString();
            string themeName = getThemeName(path);
            string themePath = getThemePath(path);
            exportWindow.SetThemeName(themeName);
            path = path.Remove(0, 8);
            int idex = path.LastIndexOf("/");
            ImageSource imageSource;
            if (currentImage != null)
            {
                //string oldPath = path.Remove(idex + 1, 0);
                imageSource = new BitmapImage(new Uri(currentPath));
                currentImage.Source = imageSource;
            }
            string newPath = path.Insert(idex + 1, "S");
            
            if (System.IO.File.Exists(newPath))
            {
                imageSource = new BitmapImage(new Uri(newPath));
                image.Source = imageSource;
                currentPath = path;
                currentImage = image;

                textTheme.Text = "CURRENT THEME: " + themeName.ToUpper();
            }

          // MessageBox.Show(themeName +  "--" +themePath);
        }

        private string getThemeName(string path)
        {
            int index = 0;
            int indexTemp =0;
            do
            {
                index = indexTemp;
                indexTemp = path.IndexOf("/", index + 2);
            }
            while (indexTemp != -1);

            int pointIndex = path.IndexOf(".");
            int themeNameLenght = pointIndex - index;
            string name = path.Substring(index+1, themeNameLenght -1);

            //string rs = path.Substring(

            return name;
        }


        private string getThemePath(string path)
        {
            int pointIndex = path.IndexOf(".");
            pointIndex = path.IndexOf(".",pointIndex +1);

            string name = path.Substring(7, pointIndex -7);
            return name;
        }

    }
}